﻿using System;
using System.Web;
using NUnit.Framework;

namespace OpenWaves.UnitTests
{
    [TestFixture]
    public class HtmlTests
    {
        [Test]
        public void TestThatTextWithNoSpecialCharactersIsWellEncoded()
        {
            Assert.AreEqual("elo melo", Html.Encode("elo melo"));
        }

        [Test]
        public void TestThatTextWithHtmlTagsIsWellEncoded()
        {
            Assert.AreEqual("&lt;b&gt;elo melo&lt;/b&gt;", Html.Encode("<b>elo melo</b>"));
        }

        [Test]
        public void TestThatTextWithNationalCharactersIsWellEncoded()
        {
            Assert.AreEqual("ż&#243;łć", Html.Encode("żółć"));
        }

        [Test]
        public void TestThatBreakLinesAreTransformedToBrTags()
        {
            Assert.AreEqual("elo<br/>melo", Html.Encode("elo\nmelo", HtmlEncodeOptions.PreserveLineBreaks));
            Assert.AreEqual("line1<br/>line2", Html.Encode("line1" + Environment.NewLine + "line2", HtmlEncodeOptions.PreserveLineBreaks));
        }

        [Test]
        public void TestThatUrlsIsWellFormated()
        {
            Assert.AreEqual("<a href=\"http://wp.pl/\">http://wp.pl</a>", Html.Encode("http://wp.pl", HtmlEncodeOptions.ActivateUrls));
            Assert.AreEqual("See: <a href=\"http://www.wp.pl/\">www.wp.pl</a>.", Html.Encode("See: www.wp.pl.", HtmlEncodeOptions.ActivateUrls));
            Assert.AreEqual("<a href=\"http://www.google.com/search?q=good%20url%20regex&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1\">http://www.google.com/search?q=good+url+regex&amp;rls=com.microsoft:*&amp;ie=UTF-8&amp;oe=UTF-8&amp;startIndex=&amp;startPage=1</a>. ss",
                Html.Encode("http://www.google.com/search?q=good+url+regex&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1. ss", HtmlEncodeOptions.ActivateUrls));
        }

        [Test]
        public void TestEncodingNorwegian()
        {
            Assert.AreEqual("M&#248;te for medlemmer av fylkesstyret Telemark", Html.Encode("Møte for medlemmer av fylkesstyret Telemark", HtmlEncodeOptions.ActivateUrls));
        }

        [Test]
        public void TestThatHtmlFormattingTagsCaseInsenitiveWorks()
        {
            Assert.AreEqual("<u>A</u>", Html.Encode("<u>A</U>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("<u>A</u>", Html.Encode("<U>A</U>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("<u>A</u>", Html.Encode("<U>A</u>", HtmlEncodeOptions.PreserveFormatting));
        }

        [Test]
        public void TestPreservingFormatting()
        {
            Assert.AreEqual("<b>elo</b>", Html.Encode("<b>elo</b>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("<b>&lt;p&gt;elo&lt;/p&gt;</b>", Html.Encode("<b><p>elo</p></b>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("elo<u>melo<b>haha</b>xxx</u>dupa", Html.Encode("elo<u>melo<b>haha</b>xxx</u>dupa", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("<u><u></u></u>", Html.Encode("<u><u></u></u>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("&lt;u&gt;&lt;u&gt;&lt;u&gt;", Html.Encode("<u><u><u>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("<u>&lt;p&gt;</u>&lt;/p&gt;", Html.Encode("<u><p></u></p>", HtmlEncodeOptions.PreserveFormatting));
            Assert.AreEqual("<u>elo<i>melo&lt;strong&gt;</i></u>&lt;/strong&gt;", Html.Encode("<u>elo<i>melo<strong></i></u></strong>", HtmlEncodeOptions.PreserveFormatting));
        }

        [Test]
        public void TestLongWordsWrapping()
        {
            Assert.AreEqual("thisIsQuiteLong", Html.Encode("thisIsQuiteLong", HtmlEncodeOptions.WrapLongWords));
            Assert.AreEqual("WordConcatenatedWith&shy;EvenALongerOne", Html.Encode("WordConcatenatedWithEvenALongerOne", HtmlEncodeOptions.WrapLongWords));
            Assert.AreEqual("aaaaaaaaaaaaa", Html.Encode("aaaaaaaaaaaaa", HtmlEncodeOptions.WrapLongWords));
            Assert.AreEqual("aaaaaaaaaaaqqqqaaaaa&shy;aaaaaa", Html.Encode("aaaaaaaaaaaqqqqaaaaaaaaaaa", HtmlEncodeOptions.WrapLongWords));
            Assert.AreEqual("kapakapakapakapakapa&shy;kapakapa<br/><br/>kapa",
                       Html.Encode("kapakapakapakapakapakapakapa\n\nkapa",
                              HtmlEncodeOptions.PreserveLineBreaks | HtmlEncodeOptions.WrapLongWords));
        }

        [Test]
        public void TestThatCompositionWorks()
        {
            Assert.AreEqual("<a href=\"http://www.onet.pl/\">http://www.onet.pl</a>",
                     Html.Encode("http://www.onet.pl", HtmlEncodeOptions.PreserveLineBreaks | HtmlEncodeOptions.ActivateUrls | HtmlEncodeOptions.WrapLongWords));

            Assert.AreEqual(
                  "aaaaaaaaaaaaaaaaaaaa&shy;aaaaa <a href=\"http://jakisdlugiurlaaaaaaaaaaaaaaaaaaaaaaaa.com/\">http://jakisdlugiurl&shy;aaaaaaaaaaaaaaaaaaaa&shy;aaaa.com</a> no &amp; resta tekstu",
                  Html.Encode("aaaaaaaaaaaaaaaaaaaaaaaaa http://jakisdlugiurlaaaaaaaaaaaaaaaaaaaaaaaa.com no & resta tekstu",
                         HtmlEncodeOptions.PreserveLineBreaks | HtmlEncodeOptions.ActivateUrls | HtmlEncodeOptions.WrapLongWords));

            Assert.AreEqual("kapakapakapakapakapa&shy;kapakapa<br/><br/>kapa",
                Html.Encode("kapakapakapakapakapakapakapa\n\nkapa",
                  HtmlEncodeOptions.PreserveLineBreaks | HtmlEncodeOptions.WrapLongWords));

            Assert.AreEqual(
                "<u>elo <a href=\"http://wp.pl/\">http://wp.pl</a></u>",
                Html.Encode("<u>elo http://wp.pl</u>", HtmlEncodeOptions.ActivateUrls | HtmlEncodeOptions.PreserveFormatting));

        }

        [Test]
        public void TestPreserveLineBraksAndPreserveFormattingCanBeComposed()
        {
            Assert.AreEqual(
                "aaa <b>bbbb</b><br/><br/>ccc",
                Html.Encode("aaa <b>bbbb</b>\n\nccc", HtmlEncodeOptions.PreserveLineBreaks | HtmlEncodeOptions.PreserveFormatting));
        }

        [Test]
        public void TestCuttingString()
        {
            Assert.AreEqual(10,
                Html.Encode("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww", HtmlEncodeOptions.None, 10).Length);
            Assert.AreEqual(40,
                Html.Encode("<<><>><<>><>><<>><", HtmlEncodeOptions.None, 10).Length);
            Assert.AreEqual("<u>elo melo<i>dupa</i></u><u></u>", Html.Encode("<u>elo melo<i>dupa</i></u>xxx<u></u>", HtmlEncodeOptions.PreserveFormatting, 12));
            Assert.AreEqual("<a href=\"http://wp.pl/\">http://wp.</a>", Html.Encode("http://wp.pl", HtmlEncodeOptions.ActivateUrls, 10));

        }

        [Test]
        public void TestThatCompositionOfTruncatingAndWrappingWorks()
        {
            Assert.AreEqual("aaaaaaaaaaaaaaaaaaaa&shy;aaaaa&shy;", Html.Encode("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", HtmlEncodeOptions.WrapLongWords, 25));
        }


        [Test]
        public void TestThatEncodingNullReturnsNull()
        {
            Assert.IsNull(Html.Encode(null));
            Assert.IsNull(Html.Encode(null, HtmlEncodeOptions.None));
            Assert.IsNull(Html.Encode(null, HtmlEncodeOptions.PreserveLineBreaks));

        }

        [Test]
        [TestCase(null, HtmlEncodeOptions.None)]
        [TestCase(null, HtmlEncodeOptions.PreserveLineBreaks)]
        [TestCase("", HtmlEncodeOptions.None)]
        [TestCase("", HtmlEncodeOptions.PreserveLineBreaks)]
        [TestCase("<a>abc</a>", HtmlEncodeOptions.None)]
        [TestCase("<a>abc</a>", HtmlEncodeOptions.PreserveLineBreaks)]
        public void TestThatEncodingIsConsistentWithHttpUtility(string value, HtmlEncodeOptions flags)
        {
            Assert.AreEqual(HttpUtility.HtmlEncode(value), Html.Encode(value, flags));
        }
    }
}